<!DOCTYPE html>
<html lang="zh-CN">
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  <title>Lua  读写卡客户端 | 在线编辑器</title> 
  <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 
  <link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet" /> 
  <script src="./CodeMirror/lib/codemirror.min.js"></script> 
  <link rel="stylesheet" href="./CodeMirror/lib/codemirror.min.css" /> 
  <script src="./CodeMirror/mode/lua/lua.js"></script> 
  <!--[if lt IE 9]>
	<script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js"></script>
	<![endif]--> 
  <style> 
	 
	  .CodeMirror {
	    font-size:14px;
	    height:450px;
	  }
	  #textareaCode {
	    height: 450px;
	  }
	  #iframeResult {
	    border: 0!important;
	    min-width: 100px;
	    width: 100%;
	    height: 450px;
	    background-color: #fff;
	  }
	  @media screen and (max-width: 768px) {
	    #textareaCode {
	      height: 300px;
	    }
	    .CodeMirror {
	      font-size:14px;
	      height:300px;
	    }
	    #iframeResult {
	     height: 300px;
	    }
	    .form-inline {
	      padding: 6px 0 2px 0;
	    }
	  }
	  .ad {
	    text-align: center;
	  }
	</style> 
  <!-- jQuery --> 
  <script src="./jquery/jquery.min.js"></script> 
 </head> 
 <body> 
  <p></p> 
   <div class="container" style="width:100%;height:100%; font-size:12px;"> 
   	<div class="row clearfix">
			<div class="col-md-12 column">
				<ul class="nav nav-tabs">
					<li class="active">
						 <a href="./runcode.php">首页</a>
					</li>
					<li>
						 <a href="./help.php">帮助</a>
					</li>
					<li>
						 <a href="./about.php">简介</a>
					</li>
				</ul>
				<div class="page-header">
					<h1>
						<small>卡片读写工具Web客户端，本工具以Lua脚本的形式对CPU卡，M1卡就行读、写等各种操作.</small>
					</h1>
				</div>
			</div>
	</div> 
    <div class="row" style="background-color: #e5eecc;"> 
     <div class="panel panel-default" style="margin-bottom:0;"> 
      <div class="panel-body" style="background-color: #e5eecc;border-color: #e5eecc;"> 
       <form autocomplete="off" role="form"> 
        <div class="row"> 
         <div class="col-sm-6"> 
          <div class="row-fluid"> 
           <label class="inline"><strong style="font-size: 16px;color:#617f10;">Lua 测卡脚本:</strong></label> 
           <input id="submitBTN" type="button" class="pull-right inline" value="提交运行 &raquo;" /> 
           <input type="hidden" id="bt" name="bt" /> 
           <input type="hidden" name="code" id="code" /> 
          </div> 
          <textarea class="form-control" id="textareaCode" name="textareaCode">--this is a demo:
----------------------------------------------------
--读卡服务地址
ip= "127.0.0.1"
--读卡服务端口
port = 5050

print("Hello My Lua!")

--连接到读卡服务
ret = CONNECT(ip,port)                     --建立连接
-------------------------------------------  CPU卡操作
ret,rcv = S_SFI("\x3F\x00",1)              --选择PSAM卡应用3F00
ret,rcv = R_BFile(0x15,0,0,1)              --读取PSAM卡15文件
ret,rcv = R_BFile(0x16,0,0,1)              --读取PSAM卡16文件
if ret == 0x9000 then
	PasmTID = string.sub(rcv,1,12)
	print("pasmTID:"..PasmTID)
end

ret,rcv = S_SFI("\x3F\x00",0)              --选择卡片应用3F00
ret,rcv = S_AID("MOT.INTERCITY01",0x0E,0)  --选择卡片应用MOT.INTERCITY01
ret,rcv = R_BFile(0x15,0,0,0)              --读取卡片0x15文件

DISCONNECT()                          --断开连接
         </textarea> 
         </div> 
         <div class="col-sm-6"> 
          <label><strong style="font-size: 16px;color:#617f10;"> 输出结果：</strong></label> 
          <div id="iframewrapper"> 
           <iframe id="iframeResult" class="result_output" frameborder="0" name="view" src="out.txt" width="400"></iframe> 
          </div> 
         </div> 
        </div> 
       </form>
      </div>  
     </div> 
    </div> 
   </div>
   <div id="footer">
      <!-- <div class="fixed-btn"> -->
        Copyright &copy; 2016-2016    <strong><a href="http://blog.csdn.net/qq8864" target="_blank"></a></strong>&nbsp;
        <strong><a href="http://blog.csdn.net/qq8864" target="_blank">newcapec</a></strong> All Rights Reserved. 
     <!--  </div> -->
   </div>    
<script> 
  var editor = CodeMirror.fromTextArea(document.getElementById("textareaCode"), {
        lineNumbers: false,
        matchBrackets: true,
        mode: "text/x-lua",
        indentUnit: 4,
        indentWithTabs: true
  });
  var ifr = document.createElement("iframe");
  ifr.setAttribute("frameborder", "0");
  ifr.setAttribute("id", "iframeResult");  
  document.getElementById("iframewrapper").innerHTML = "";
  document.getElementById("iframewrapper").appendChild(ifr);
  var ifrw = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
  ifrw.document.open();
  ifrw.document.write("<html><head><\/head><body>Hello World11!<br><\/body><\/html>");  
  ifrw.document.close();
  $("#submitBTN").click(function() {
  code = editor.getValue();
  runcode = 17;
  loadingdata = '脚本执行中...';
  loadingdata += '<br><img src="./loading.gif">';

  ifrw.document.open();
  ifrw.document.write(loadingdata);  
  ifrw.document.close();
  $.post("./compile.php",{code:code,language:runcode},function(data){
  	//text = JSON.parse(data).output.replace(/\r\n|\r|\n/g,"<br />")  + JSON.parse(data).errors;
  	text = data.output.replace(/\r\n|\r|\n/g,"<br />")  + data.errors;
	ifrw.document.open();
	ifrw.document.write(text);  
	ifrw.document.close();
  });
  
})
</script>   
</body>
</html>